<template>
  <a-modal :visible="visible" title="添加" :width="700" forceRender ok-text="确认" cancel-text="取消" @ok="onSubmit" @cancel="onClose">
    <a-form ref="formRef" :model="form" :rules="rules" >
      <a-form-item label="事项名称" name="memoName">
        <a-input v-model:value="form.memoName" placeholder="请输入事项名称" />
      </a-form-item>
        <a-form-item label="事项类型" name="memoType">
          <DictSelectType :service-id="SERVICE_ID_ENUM.DICTIONARY.value" :dcit-type="DICT_TYPE_ENUM.TM_MEMO_TYPE.value" v-model:value="form.memoType" placeholder="事项类型" />
        </a-form-item>
        <a-form-item label="事项人员" name="userId">
          <DictSelect :service-id="SERVICE_ID_ENUM.USER.value" v-model:value="form.userId"  :showSearch="true" :allowClear="true" placeholder="事项人员" />
        </a-form-item>
        <a-form-item label="关联项目" name="projectId">
        <ProjectSelectTree  v-model:value="form.projectId"  />
        </a-form-item>
      <a-form-item label="备注" name="remark">
        <a-textarea v-model:value="form.remark" placeholder="请输入备注" :rows="3" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script setup lang="ts">
  import { message } from 'ant-design-vue';
  import _, { fromPairs } from 'lodash';
  import { nextTick, reactive, ref } from 'vue';
  import { memoApi } from '/@/api/memo-api/memo-api';
  import { SERVICE_ID_ENUM,DICT_TYPE_ENUM } from '/@/constants/system/cache-service-id-const';
  import DictSelect from '/@/components/cache/dict-select.vue'
  import DictSelectType from '/@/components/cache/dict-select-type.vue'
  import { SmartLoading } from '/@/components/framework/smart-loading';
  import { smartSentry } from '/@/lib/smart-sentry';
  import ProjectSelectTree from '/@/components/business/task/project-select-tree.vue';
  import HasUsersSelect from '/@/components/business/task/has-users-select.vue';

  defineExpose({
    showModal,
  });
  const emit = defineEmits('refresh');

  // --------------------- modal 显示与隐藏 ---------------------
  // 是否展示
  const visible = ref(false);
  function showModal(record) {
    if(record){
      Object.assign(form, record);
    }
    else{
      Object.assign(form, formDefault);
    }
    visible.value = true;
  }

  function onClose() {
    visible.value = false;
  }

  // --------------------- 表单 ---------------------

  //  组件
  const formRef = ref();

  const formDefault = {
    id: undefined,
    memoName: undefined,
    memoType: undefined,
    projectId: undefined,
    userId: undefined,
    remark: undefined,
  };
  let form = reactive({ ...formDefault });
  const rules = {
    memoName: [{ required: true, message: '请输入事项名称' }],
    memoType: [{ required: true, message: '请输入事项类型' }],
    userId: [{ required: true, message: '请输入负责人' }],
  };

  function onSubmit() {
    formRef.value
      .validate()
      .then(async () => {
        SmartLoading.show();
        try {
          let res=await memoApi.save(form);
          message.success(res.message);
          emit('refresh');
          onClose();
        } catch (error) {
          smartSentry.captureError(error);
        } finally {
          SmartLoading.hide();
        }
      })
      .catch((error) => {
        console.log('error', error);
        message.error('参数验证错误，请仔细填写表单数据!');
      });
  }
</script>

<style lang="less" scoped>
  .form-width {
    width: 100% !important;
  }

  .footer {
    width: 100%;
    display: flex;
    justify-content: flex-end;
  }

  :deep(.ant-card-body) {
    padding: 10px;
  }
</style>
